initial stab at a zeroconf unified event stream#2
Conversation
| return # no change | ||
| # Address changed — disconnect stale connection and reconnect. | ||
| await api.disconnect() | ||
| self._plug_apis.pop(mac) |
There was a problem hiding this comment.
is there a race here, since the await may block for some time and conceivably we'd get _plug_discovered() called again with the same mac prior to the first disconnect finishing? If so, we should pop() it before we await, yes?
| async def _plug_lost(self, mac: str) -> None: | ||
| """Called by the subclass when a plug has definitively disappeared.""" | ||
| if mac in self._plug_apis: | ||
| await self._plug_apis.pop(mac).disconnect() |
| await api.disconnect() | ||
| self._plug_apis.pop(mac) | ||
|
|
||
| await self._add_device(mac, 'plug') |
There was a problem hiding this comment.
Do we need a prior self._remove_device() if we already had it? To keep events symmetrical?
| await self._add_device(mac, 'sensor') | ||
| if self._relay_now_relaying_for and self._event_cb is not None: | ||
| obj['event'] = ev | ||
| await self._event_cb(obj) |
There was a problem hiding this comment.
Does this not need a device.subscribed check?
|
|
||
| async def yourcallback(event: dict) -> None | ||
|
|
||
| Known events: |
There was a problem hiding this comment.
Where did the documentation for known events end up after the refactor?
| Usage example (HA — pass the shared zeroconf instance):: | ||
|
|
||
| zc = await homeassistant.components.zeroconf.async_get_instance(hass) | ||
| devices = PowersensorZeroconfDevices(zeroconf_instance=zc) |
There was a problem hiding this comment.
__init__ does not seem to take zeroconf_instance as a named parameter, just a positional one?
| self._start_expiry_timer() | ||
|
|
||
| if self._zc_instance is None: | ||
| self._zc_instance = _zc.Zeroconf() |
There was a problem hiding this comment.
is this not initialisation that belongs in __init__()?
| self._zc_instance = _zc.Zeroconf() | ||
|
|
||
| loop = asyncio.get_running_loop() | ||
| self._listener = _Listener(self, loop) |
There was a problem hiding this comment.
Do we need a check to handle the case where someone calls start() multiple times? Aren't we just leaving prior instances dangling otherwise?
| info = _zc.ServiceInfo(type_, name) | ||
| if not info.load_from_cache(zc): | ||
| _LOGGER.warning( | ||
| "No cache entry for %s — device will appear on next mDNS announcement", |
There was a problem hiding this comment.
Is this bad? Can it be avoided? What would I need to do differently to avoid scary log messages?
| To use mDNS-based discovery, install the zeroconf extra:: | ||
|
|
||
| pip install powersensor-local[zeroconf] | ||
| """ |
There was a problem hiding this comment.
wow, fancy and user friendly!
bcaf15c to
06d906c
Compare
No description provided.